/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package sensorgui;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.swing.SwingUtilities;

/**
 *
 * @author Conor
 */
public class Grid {

 private int xSquares;
 private int ySquares;

 private int squareHeight = 200;
 private int squareWidth = 150;

 private Map<Sensor, SensorView>  sensorsAndView = new HashMap();
 private Square[][] contents;


 public Grid(int x, int y){
        xSquares = x;
        ySquares = y;

        //sensors = new ArrayList(((x+1)*2) + ((y-1)*2));
        contents = new Square[x][y];
        initGrid();

 }

 public int getXSquares(){
     return this.xSquares;
 }

 public int getYSquares(){
     return this.ySquares;
 }

 public int getSquareHeight(){
     return this.squareHeight;
 }

 public int getSquaresWidth(){
     return this.squareWidth;
 }

 public void initGrid(){

     for (int i=0; i<xSquares; i++ ){
         for (int j=0; j<ySquares; j++){
            contents[i][j] = new Square(new GridPoint(i,j));
         }
     }
     
     this.addNorthFacingSensors();
     this.addSouthFacingSensors();
     this.addEastFacingSensors();
     this.addWestFacingSensors();
     this.addCornerSensors();

 }

 public void addSouthFacingSensors(){

     int northPerimeter = 0;
     int xPos = this.squareWidth;

     for (int i=1; i<xSquares; i++){
         Sensor aSensor = new Sensor(xPos,northPerimeter,60,SensorConstants.south, this);
         aSensor.setDirection(new Direction(SensorConstants.south));
         aSensor.setWidth(this.squareWidth);
         
         SensorView sv = new SensorView(aSensor);
         sensorsAndView.put(aSensor, sv);
         xPos = xPos + this.squareWidth;
     }

 }

 public void addNorthFacingSensors(){

     int southPerimeter = this.squareHeight*this.ySquares;
     int xPos = this.squareWidth;

     for (int i=1; i<xSquares; i++){
         Sensor aSensor = new Sensor(xPos,southPerimeter,60,SensorConstants.north, this);
         //aSensor.setDirection(new Direction(SensorConstants.north));
         aSensor.setWidth(this.squareWidth);
         
         SensorView sv = new SensorView(aSensor);
         sensorsAndView.put(aSensor, sv);
         xPos = xPos + this.squareWidth;
     }

 }

 public void addWestFacingSensors(){
    int eastPerimeter = this.squareWidth*this.xSquares;
     int yPos = this.squareHeight;

     for (int i=1; i<ySquares; i++){
         Sensor aSensor = new Sensor(eastPerimeter,yPos,70,SensorConstants.west, this);
         //aSensor.setDirection(new Direction(SensorConstants.west));
         aSensor.setWidth(this.squareWidth);
         
         SensorView sv = new SensorView(aSensor);
         sensorsAndView.put(aSensor, sv);
         yPos = yPos + this.squareHeight;
     }
 }

 public void addEastFacingSensors(){
    int westPerimeter = 0;
     int yPos = this.squareHeight;

     for (int i=1; i<ySquares; i++){
         Sensor aSensor = new Sensor(westPerimeter,yPos,70,SensorConstants.east, this);
         //aSensor.setDirection(new Direction(SensorConstants.east));
         aSensor.setWidth(this.squareWidth);
         
         SensorView sv = new SensorView(aSensor);
         sensorsAndView.put(aSensor, sv);
         yPos = yPos + this.squareHeight;
     }
 }

  public void addCornerSensors(){
    int westPerimeter = 0;
    int eastPerimeter = this.squareWidth*this.xSquares;
    int northPerimeter = 0;
    int southPerimeter = this.squareHeight*this.ySquares;

    int span = 20;
    
    Sensor nwSensor = new Sensor(westPerimeter,northPerimeter,span,SensorConstants.southeast, this);
    //nwSensor.setDirection(new Direction(SensorConstants.southeast));
    nwSensor.setWidth(this.squareWidth);
    SensorView sv = new SensorView(nwSensor);
    sensorsAndView.put(nwSensor, sv);

    Sensor neSensor = new Sensor(eastPerimeter,northPerimeter,span,SensorConstants.southwest, this);
    //neSensor.setDirection(new Direction(SensorConstants.southwest));
    neSensor.setWidth(this.squareWidth);
    
    sv = new SensorView(neSensor);
    sensorsAndView.put(neSensor, sv);

    Sensor swSensor = new Sensor(westPerimeter,southPerimeter,span,SensorConstants.northeast, this);
    //swSensor.setDirection(new Direction(SensorConstants.northeast));
    swSensor.setWidth(this.squareWidth);
    sv = new SensorView(swSensor);
    sensorsAndView.put(swSensor, sv);

    Sensor seSensor = new Sensor(eastPerimeter,southPerimeter,span,SensorConstants.northwest, this);
    //seSensor.setDirection(new Direction(SensorConstants.northwest));
    seSensor.setWidth(this.squareWidth);
    sv = new SensorView(seSensor);
    sensorsAndView.put(seSensor, sv);
    
 }

 public List<Sensor> getSensors(){

   List<Sensor> list = new ArrayList<Sensor>(sensorsAndView.keySet());  
   return list;
 }
 
 public SensorView getSensorsView(Sensor sensor){
  
	   return (SensorView)this.sensorsAndView.get(sensor);
 }
 
 public void show() {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				new AppView(Grid.this);
			}
		});
	}

}
